import PaBaseModel from '../../core/model/PaBaseModel';

function documentItemMgtCtrl($scope, $http, ngDialog, ngDialogConfirm) {
  'ngInject';
  $scope.type = $state.params.type;
  $scope.id = $state.params.id;
  $scope.imgUrl = $state.params.imgUrl;
  $scope.name = $state.params.name;
  $scope.taskId = $state.params.taskId;

  this.getImgUrl = () => {
    let imgUrl = $scope.imgUrl;
    return require(`../../../assets/images/${imgUrl}`);
  };

  // 二级菜单
  this.routinePatrol=[];
  this.initDocumentMgtView = () => {
    // 获得二级菜单
    if($scope.name == '检查评价'){
      this.routinePatrol = [
        {
          id:'01',
          checkCategoryName:'专项检查',
          stateName: 'documentItemMgt.specialCheck',
        },
        {
          id:'02',
          checkCategoryName:'日常巡查',
          stateName: 'documentItemMgt.dailyCheck',
        },
        // {
        //   id:'03',
        //   checkCategoryName:'带班日志',
        //   stateName: 'documentItemMgt.crewScheduling',
        // },
        {
          id:'03',
          checkCategoryName:'工作日志',
          stateName: 'documentItemMgt.commonDocument',
        },
        {
          id:'04',
          checkCategoryName:'检查通报',
          stateName: 'documentItemMgt.commonDocument',
        },
        {
          id:'05',
          checkCategoryName:'其他',
          stateName: 'documentItemMgt.commonDocument',
        },
      ];
    } else if ($scope.name == '安全活动') {
      this.routinePatrol = [
        {
          id:'01',
          checkCategoryName:'教育培训',
          stateName: 'documentItemMgt.techTraining',
        },
        {
          id:'02',
          checkCategoryName:'安全交底',
          stateName: 'documentItemMgt.safeDisclosure',
        },
        {
          id:'03',
          checkCategoryName:'安全会议',
          stateName: 'documentItemMgt.meeting',
        },
      ];
    }
  };
  this.initDocumentMgtView();

  this.selectFolder = (index) => {
    this.routinePatrol.forEach((item) => {
      item.active = false;
    });

    let activeMenu = this.routinePatrol[index];
    activeMenu.active = true;

    $state.go(activeMenu.stateName, {
      id: $scope.id,
      type: 'detail',
      imgUrl: $scope.imgUrl,
      name: $scope.name,
      commonType: activeMenu.stateName == 
        'documentItemMgt.commonDocument' ? activeMenu.checkCategoryName : '',
    });

  };

  // 进入二级菜单
  if (!this.routinePatrol.length) {
    let stateName = 'documentItemMgt.commonDocument';

    if ($scope.name == '人员管理') {
      stateName = 'documentItemMgt.workerInfo';
    } else if ($scope.name == '机械设备') {
      stateName = 'documentItemMgt.equipmentInfo';
    }
    $state.go(stateName, {
      id: $scope.id,
      type: $scope.type,
      imgUrl: $scope.imgUrl,
      name: $scope.name,
    });
  } else {
    this.selectFolder(0);
  }

  // 评分标准数据
  this.evaluationStandardList = [];
  // 获得评分标准数据方法
  this.getEvaluationStandardList = () => {
    $http.get(`${SERVER_CONTEXT}/api/safeCheck/safecheck/${$scope.id}`)
      .then((resp) => {
        this.evaluationStandardList = resp.data;
        this.evaluationStandardList.forEach((item) => {
          item.value = 0;
        });
      });
  }

  // 提交打分记录
  this.submitScore = () => {
    ngDialogConfirm.confirm('提交内页审核记录', '提交记录后无法更改,确认提交吗?')
      .then(() => {
        let reqData = {
          scoringRecordBeanList: [],
        };
        this.evaluationStandardList.forEach((item) => {
          let record = {};
          record.taskId = $scope.taskId;
          record.checkItemId = item.id;
          record.score = item.value;
          reqData.scoringRecordBeanList.push(record);
        });
        $http.post(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/batchAddScoringRecords`,
          reqData)
            .then((resp) => {
              if (resp.data.success) {
                layer.msg(resp.data.message);
                $state.go('documentInside', {
                  id: $scope.taskId,
                });
              } else {
                layer.alert(resp.data.message);
              }
            });
      });
  };

  // 重置打分记录
  this.resetScore = () => {
    this.evaluationStandardList.forEach((item) => {
      item.value = 0;
    });
  }

  if ($scope.type == 'check') {
    this.getEvaluationStandardList();
  }

}

export default documentItemMgtCtrl;